
import 'package:flutter/material.dart';
import 'package:flutter_easyrefresh/easy_refresh.dart';
import 'package:jubobao/home/JuBoNewModel.dart';
import 'package:jubobao/home/JuBoNewWebView.dart';
import 'package:jubobao/other/Widgets/JuBoCommonWidget.dart';

import 'JuBo_home_services.dart';

class JuBoNews extends StatefulWidget {

  static final routeName = 'JuBoNews';

  @override
  _JuBoNewsState createState() => _JuBoNewsState();
}

class _JuBoNewsState extends State<JuBoNews> {

  List<JuBoNewModel> _result;
  int _pageSize = 1;

  @override
  void initState() {
    // TODO: implement initState
    super.initState();
    _loadData(_pageSize);
  }

   _loadData(index){
    JuBoHomeRequest.newsConfig(index).then((value){
      setState(() {
        if(index == 1){
          _result = value;
        }else{
          for(JuBoNewModel model in value){
            _result.add(model);
          }
        }
      });
    });
  }

  @override
  Widget build(BuildContext context) {
    return Scaffold(
      appBar: AppBar(title: Text('聚玻资讯'),),
      body: _result == null ? JuBoWidget.loadingWidget() :EasyRefresh(
        onRefresh: () async {
          _pageSize = 1;
         await _loadData(_pageSize);
        },
        onLoad: () async {
          _pageSize++;
          await _loadData(_pageSize);
        },
        child: ListView.separated(
            itemBuilder: (ctx,index){
              return item(_result[index]);
            },
            itemCount: _result.length,
            separatorBuilder: (ctx,index){
              return Divider();
            },
        ),
      ),
    );
  }

  Widget item(JuBoNewModel newModel){
    return InkWell(
      onTap: (){
        print('聚玻资讯 哈哈哈哈');
        Navigator.of(context).push(MaterialPageRoute(
              builder: (ctx){
                return JuBoNewWebView(newModel.webUrl);
              }
        ));
      },
      child: Padding(
        padding: const EdgeInsets.all(8.0),
        child: Column(
          crossAxisAlignment: CrossAxisAlignment.start,
          children: <Widget>[
            Text(newModel.title,
               style: TextStyle(color: Colors.black,fontSize: 16),
            ),
               SizedBox(height: 8,),
               Text(newModel.createTime)
          ],
        ),
      ),
    );
  }
}

